#include "FreeRTOS.h"
#include "task.h"
#include "app_freertos.h"


/* task handler */
TaskHandle_t APP_MAIN_handle;
TaskHandle_t UART_TASK_handle;
TaskHandle_t ISP_TASK_handle;

/* variables for task tcb and stack */
StackType_t app_main_stack[512];
StackType_t UART_TASK_stack[256];
StackType_t ISP_TASK_stack[512];
StaticTask_t my_task01_buffer;
StaticTask_t usb_task02_buffer;
StaticTask_t ISP_TASK_buffer;

/* Idle task control block and stack */
static StackType_t idle_task_stack[configMINIMAL_STACK_SIZE];
static StaticTask_t idle_task_tcb;

/* External Idle and Timer task static memory allocation functions */
extern void vApplicationGetIdleTaskMemory( StaticTask_t ** ppxIdleTaskTCBBuffer, StackType_t ** ppxIdleTaskStackBuffer, uint32_t *pulIdleTaskStackSize );

/*
  vApplicationGetIdleTaskMemory gets called when configSUPPORT_STATIC_ALLOCATION
  equals to 1 and is required for static memory allocation support.
*/
void vApplicationGetIdleTaskMemory( StaticTask_t ** ppxIdleTaskTCBBuffer, StackType_t ** ppxIdleTaskStackBuffer, uint32_t *pulIdleTaskStackSize )
{
  *ppxIdleTaskTCBBuffer = &idle_task_tcb;
  *ppxIdleTaskStackBuffer = &idle_task_stack[0];
  *pulIdleTaskStackSize = (uint32_t)configMINIMAL_STACK_SIZE;
}


/**
  * @brief  initializes all task.
  * @param  none
  * @retval none
  */
void freertos_task_create(void)
{
  /* create the APP_MAIN task by static */
  APP_MAIN_handle = xTaskCreateStatic(app_main,
                                       "APP_MAIN",
                                       512,
                                       NULL,
                                       0,
                                       app_main_stack,
                                       &my_task01_buffer);

  /* create the UART_TASK task by static */
  UART_TASK_handle = xTaskCreateStatic(uart_task_func,
                                       "UART_TASK",
                                       256,
                                       NULL,
                                       0,
                                       UART_TASK_stack,
                                       &usb_task02_buffer);

  /* create the ISP_TASK task by static */
  ISP_TASK_handle = xTaskCreateStatic(ISP_task_func,
                                       "ISP_TASK",
                                       512,
                                       NULL,
                                       0,
                                       ISP_TASK_stack,
                                       &ISP_TASK_buffer);
}


void wk_freertos_init(void)
{
    taskENTER_CRITICAL();
    
    freertos_task_create();
    
    taskEXIT_CRITICAL();
    
    vTaskStartScheduler();
}


__WEAK void app_main(void *pvParameters)
{
    while(1)
    {
        printf("__WEAK void app_main(void *pvParameters) \r\n");
        vTaskDelay(500);
    }
}

__WEAK void uart_task_func(void *pvParameters)
{
    while(1)
    {
        printf("__WEAK void uart_task_func(void *pvParameters) \r\n");
        vTaskDelay(500);
    }
}


__WEAK void ISP_task_func(void *pvParameters)
{
    while(1)
    {
        printf("__WEAK void ISP_task_func(void *pvParameters) \r\n");
        vTaskDelay(500);
    }
}

















